//
//  BKManyClassfiyKindsHeaderView.m
//  bestkeep
//
//  Created by utouu_mhm on 16/11/14.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKManyClassfiyKindsHeaderView.h"
#import "Masonry.h"
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1]

@interface BKManyClassfiyKindsHeaderView ()

@property (nonatomic,copy) NSMutableArray *BtnMutableArray;
@property (nonatomic,strong) UIButton *seletcdBtn;
@property (nonatomic,strong) NSMutableArray *BtnArray;
@property (nonatomic,assign) BOOL isSeleted;
@property (nonatomic,strong) UIButton *priceLowBtn;

@end
@implementation BKManyClassfiyKindsHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createBtn];
        self.isSeleted = NO;
    }
    return self;
}
-(void)createBtn
{
    //    UIView *topLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 0.5)];
    //    topLineView.backgroundColor = RGB(238, 238, 238);
    //    [self addSubview:topLineView];
    
    NSArray *BtnArray = [NSArray arrayWithObjects:@"默认",@"销量",@"新品",@"价格", nil];
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - 1.5) / 4.0;
    CGFloat x = [UIScreen mainScreen].bounds.size.width/4.0;
    _BtnArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 4; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * x, 1, width, 34);
        [btn setTitle:BtnArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:RGB(64, 64, 64) forState:UIControlStateSelected];
        [btn setTitleColor:RGB(136, 136, 136) forState:UIControlStateNormal];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
        btn.tag = 1000 + i;
        [btn addTarget:self action:@selector(targetAction:) forControlEvents:UIControlEventTouchUpInside];
        UIView *separateLine = [[UIView alloc]initWithFrame:CGRectMake((i + 1) * width + 0.5, 5.5, 0.5, 25)];
        separateLine.backgroundColor = RGB(218, 218, 218);
        if (i == 1) {
            separateLine.frame = CGRectMake(ScreenWidth * 0.5, 5.5, 0.5, 25);
        }
        
        [self addSubview:btn];
        [self addSubview:separateLine];
        [_BtnArray addObject:btn];
        if (i == 0) {
            btn.selected = YES;
            self.seletcdBtn = btn;
        }
        if (i == 3) {
            UIButton *lowHighBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [lowHighBtn addTarget:self action:@selector(targetAction:) forControlEvents:UIControlEventTouchUpInside];
            lowHighBtn.backgroundColor = [UIColor whiteColor];
            lowHighBtn.tag = 2000;
            UIImage *img = [UIImage imageNamed:@"UpAndDownArrow"];
            [lowHighBtn setBackgroundImage:img forState:UIControlStateNormal];
            [btn addSubview:lowHighBtn];
            [lowHighBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(btn.mas_centerY);
                make.width.equalTo(@(10));
                make.height.equalTo(@(10));
                make.left.equalTo(btn.mas_centerX).offset(20);
            }];
            separateLine.hidden = YES;
            self.priceLowBtn = lowHighBtn;
            
        }
    }
    UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0, 34, ScreenWidth, 5)];
    bottomLineView.backgroundColor = RGB(238, 238, 238);
    [self addSubview:bottomLineView];
}
-(void)targetAction:(UIButton *)Btn
{
    if (Btn.tag == 2000 || Btn.tag == 1003) {
        [self changeBtnImageWithStatus:self.isSeleted];
        Btn.selected = YES;
        self.seletcdBtn.selected = NO;
        self.seletcdBtn = Btn;
        return;
    }else{
        self.isSeleted = NO;
        [self.priceLowBtn setBackgroundImage:[UIImage imageNamed:@"UpAndDownArrow"] forState:UIControlStateNormal];
    }
    
    if (Btn == self.seletcdBtn) {
        return;
    }else{
        Btn.selected = YES;
        self.seletcdBtn.selected = NO;
        self.seletcdBtn = Btn;
    }
    
    if ([self.HeaderViewDelegate respondsToSelector:@selector(BKManyClassfiyKindsHeaderView:index:)]) {
        
        [self.HeaderViewDelegate BKManyClassfiyKindsHeaderView:self index:Btn.tag - 1000];
    }
}

-(void)changeBtnImageWithStatus:(BOOL)isSeleted
{
    NSString *oderWay = @"";
    if (isSeleted) {
        
        oderWay = @"desc";
        self.isSeleted = NO;
        [self.priceLowBtn setBackgroundImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    }else{
        oderWay = @"asc";
        self.isSeleted = YES;
        [self.priceLowBtn setBackgroundImage:[UIImage imageNamed:@"arrowUp"] forState:UIControlStateNormal];
    }
    
    if([self.HeaderViewDelegate respondsToSelector:@selector(BKManyClassfiyKindsHeaderView:oderWay:)])
    {
        [self.HeaderViewDelegate BKManyClassfiyKindsHeaderView:self oderWay:oderWay];
    }
}
-(void)setStatus:(BKRecordGoodsListStatusModel *)status
{
    _status = status;
    
}
//切换商品列表时，重置搜索条件选项 置为全部

-(void)setIsRest:(BOOL)isRest
{
    if (isRest) {
        
        [self resetFirstButtonAndPriceBtn];
    }
    
}
-(void)resetFirstButtonAndPriceBtn
{
    for (UIButton * btn in self.BtnArray) {
        
        if (btn.tag == 1000) {
            
            btn.selected = YES;
            self.seletcdBtn = btn;
            
            UIImage *badk =[UIImage imageNamed:@"UpAndDownArrow"];
            [_priceLowBtn setBackgroundImage:badk forState:(UIControlStateNormal)];
            
            
        } else {
            
            btn.selected = NO;
        }
    }
    
}

-(void)recordSearchConditionWithSearchCondition:(NSString *)searchCondition order:(NSString *)order
{
    
    for (UIButton * btn in self.BtnArray) {
        
        if ([searchCondition isEqualToString:@""] && btn.tag == 1000) {
            
            btn.selected = YES;
            self.seletcdBtn = btn;
            
        }
        else if ([searchCondition isEqualToString:@"saledCount"] && btn.tag == 1001) {
            
            btn.selected = YES;
//            self.seletcdBtn.selected = NO;
            self.seletcdBtn = btn;
            
            
        }else if ([searchCondition  isEqualToString:@"newGoods"] && btn.tag == 1002) {
            
            btn.selected = YES;
//            self.seletcdBtn.selected = NO;
            self.seletcdBtn = btn;
            
            
        } else if([searchCondition  isEqualToString:@"price"] && btn.tag == 1003){
            
            btn.selected = YES;
//            self.seletcdBtn.selected = NO;
            self.seletcdBtn = btn;
            
            
        }else{
            btn.selected = NO;
            
        }
        
        if ([order isEqualToString:@"desc"]) {
            
            [_priceLowBtn setBackgroundImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
        }
        
        if ([order isEqualToString:@"asc"]) {
            
            [_priceLowBtn setBackgroundImage:[UIImage imageNamed:@"arrowUp"] forState:UIControlStateNormal];
            
        }
        
        if ([order isEqualToString:@""]) {
            
            [_priceLowBtn setBackgroundImage:[UIImage imageNamed:@"UpAndDownArrow"] forState:UIControlStateNormal];
        }
        
        
    }
    
}

@end
